#include <iostream>
#include "httplib.h"
#include "add_contact.pb.h"

using namespace std;
// using std::cout;
// using std::endl;
// using std::cerr;
using namespace httplib;

class ContactsException
{
private:
    std::string message;

public:
    ContactsException(std::string str = "A problem") : message(str) {};

    // 返回异常原因
    std::string what() const
    {
        return message;
    }
};

int main()
{
    cout << "-----------服务启动-----------" << endl;
    Server server;

    server.Post("./test-post", [](const Request &req, Response &res)
                {
                    cout << "接受到post请求!" << endl;
                    add_contact::AddContactRequest request;
                    add_contact::AddContactResponse response;
                    try
                    {
                        // 反序列化req.body
                        if (!request.ParseFromString(req.body))
                        {
                            throw ContactsException("AddContactRequest反序列化失败!");
                        }

                        // 新增联系人，持久化存储通讯录---> 打印新增的联系人
                        printContact(request);

                        // 构造response:res.body
                        response.set_success(true);
                        response.set_uid(1);

                        // res.body(序列化response)
                        string response_str;
                        if (!response.SerializeToString(&response_str))
                        {
                            throw ContactsException("AddContactResponse序列化失败!");
                        }
                        res.status = 200;
                        res.body = response_str;
                        res.set_header("Content-Type", "application/protobuf");
                    }
                    catch (const ContactsException &e)
                    {
                        res.status = 500;
                        response.set_success(false);
                        response.set_error_desc(e.what());
                        string response_str;
                        if (response.SerializeToString(&response_str))
                        {
                            res.body = response_str;
                            res.set_header("Content-Type", "application/protobuf");
                        }
                        cout << "contacts/add 发生异常，异常信息：" << e.what() << endl;
                    }
                });

    // 绑定8123端口,并且将端口对外开放
    server.listen("0.0.0.0", 8123);

    return 0;
}

void printContact(add_contact::AddContactRequest &req)
{
    cout << "联系人姓名：" << req.name() << endl;
    cout << "联系人年龄：" << req.age() << endl;
    for (int j = 0; j < req.phone_size(); j++)
    {
        const add_contact::AddContactRequest_Phone &phone = req.phone(j);
        cout << "联系人电话" << j + 1 << ":" << phone.phone_number() << endl;
        // 联系人电话类型
        cout << "(" << phone.PhoneType_Name(phone.type()) << ")" << endl;
    }
}

// int main(){
//     cout << "-----------服务启动-----------" << endl;
//     Server server;

//     server.Post("./test-post",[](const Request& req,Response& res){
//         cout << "接受到post请求!" << endl;
//         res.status = 200;
//     });

//     server.Get("./test-get",[](const Request& req,Response& res){
//         cout << "接受到get请求!" << endl;
//         res.status = 200;
//     });

//     //绑定8123端口,并且将端口对外开放
//     server.listen("0.0.0.0",8123);

//     return 0;
// }
